#include <head.h>
#include "./wrap.h"
void fn(char *p);

int main(int argc, char **argv)
{
    char buf[100];
    int serversocket = Socket(AF_INET, SOCK_DGRAM, 0);
    struct sockaddr_in addr;
    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(9000);
    addr.sin_addr.s_addr = htonl(INADDR_ANY);
    int r;
    // int r=  inet_pton(AF_INET,"127.0.0.1",&(addr.sin_addr.s_addr));
    // if(r ==0){
    //     sys_error("inet_pton");
    // }
    r = Bind(serversocket, (struct sockaddr *)&addr, sizeof(addr));
    struct sockaddr_in client;
    while (1)
    {
        socklen_t l = sizeof(client);
        memset(&client, 0, sizeof(client));
        CLEAR_ARRAY(buf);
        int len = recvfrom(serversocket,buf,sizeof(buf),0,(struct sockaddr*)&client,&l );
        if(len==-1){
            continue;
        }
        if(len>0){
            char addr_buf[20];
            CLEAR_ARRAY(addr_buf);
            printf("New connection from IP:%s,port:%d\n", inet_ntop(AF_INET, &(client.sin_addr), addr_buf, sizeof(addr_buf)), ntohs(client.sin_port));
            write(STDOUT_FILENO, buf, len);
            fn(buf);
            sendto(serversocket, buf, len,0,(struct sockaddr*)&client,l);
        }
    }
    close(serversocket);
    return 0;
}
void fn(char *p)
{
    if (!p)
    {
        return;
    }
    while (*p)
    {
        *p = toupper(*p);
        p++;
    }
}
